#!/usr/bin/env bash
# use root!!!
GOVERSION=1.20.2
DOCKERCOMPOSEVERSION=1.29.2
# 用户名, 由于需要使用sudo 执行脚本，所以$USER这类变量是root
echo -n "enter your name: "
read USER_NAME
# apt source 
echo 'change source'
mv /etc/apt/sources.list /etc/apt/sources.list.bak
touch /etc/apt/sources.list
echo '
deb http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multivers
deb http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
' >> /etc/apt/sources.list
apt-get update
echo 'source ok'
#make
echo "install make"
apt-get install -y make
echo "make ok"


# Git
echo "install Git"
apt-get install -y git
git config --global user.name "$USER_NAME"
echo "Git ok"

# Docker
echo "install docker..."
curl -sSL https://get.daocloud.io/docker | sh
#将登陆用户加入到docker用户组中
gpasswd -a $USER_NAME docker
#更新用户组
# newgrp docker
touch /etc/docker/daemon.json
echo '
{
    "registry-mirrors": ["https://docker.mirrors.ustc.edu.cn"]
}
' >> /etc/docker/daemon.json
systemctl restart docker
echo "docker ok"


# docker-compose
echo "install docker-compose"
curl -L https://get.daocloud.io/docker/compose/releases/download/"$DOCKERCOMPOSEVERSION"/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
# curl -L "https://github.com/docker/compose/releases/download/1.27.3/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
echo "docker-compose ok"

# go
echo "install go"
wget -P /usr/local/ https://dl.google.com/go/go"$GOVERSION".linux-amd64.tar.gz
cd /usr/local
tar -zxf go"$GOVERSION".linux-amd64.tar.gz && rm go"$GOVERSION".linux-amd64.tar.gz
# TODO(bootun): root用户的根目录不在/home下
echo '
# GOROOT
export GOROOT=/usr/local/go
# GOPATH
export GOPATH=/home/$USER_NAME/go

# PATH
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
' >> /home/$USER_NAME/.bashrc
echo 'go env -w GO111MODULE=on'
echo 'go env -w GOPROXY=https://goproxy.cn,direct'
echo "go ok"

# protobuf
echo 'install protobuf-compiler'
apt-get install -y protobuf-compiler
echo 'protobuf-compiler ok'

# grpc-go
echo 'install gRPC-go'
go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest
go install github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-grpc-gateway@latest
go install github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2@latest
go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest
echo 'gRPC-go ok'

#npm nodejs
echo "install npm and nodejs"
apt-get install -y npm nodejs
npm config set registry https://registry.npm.taobao.org
echo "npm and nodejs ok"

echo "install pm2"
npm install -g pm2
echo "pm2 ok"


# tools
echo "install tools"
echo "install cloc"
apt-get install -y cloc
echo "install net-tools"
apt-get install net-tools
echo 'tools ok'

echo "All complete!"